﻿using System;
using System.Collections.Generic;
using System.Text;
using JRBase;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace JRTestGame
{
    public class Player : Actor
    {
        private float jumpForce;
        private float jumping;
        private bool amIJumping;
        private bool hit;
        public int Speed { get; private set; }
        public bool Hit
        {
            get { return hit; }
            set { hit = value; }
        }

        private KeyboardState curKeyState;
        private int blinkingTime;
        private int blinked;
        private int hitGameTime;
        private PlayerInfo playerInfo;

        public Vector2 lastPos { get; set; }

        public void Initialize(Texture2D texture, Rectangle rectangle,
            int frameCount, int speed, PlayerInfo playerInfo)
        {
            jumpForce = 18.0f;
            jumping = jumpForce;
            hit = false;
            amIJumping = false;
            Speed = speed;
            this.playerInfo = playerInfo;

            blinkingTime = 500;
            blinked = 0;
            hitGameTime = -1;

            AnimatedSprite sprite = new AnimatedSprite();
            sprite.Initialize(texture, new Vector2(rectangle.X, rectangle.Y), rectangle.Width,
                rectangle.Height, frameCount, 30, Color.White, 1f, true);
            base.Initialize(sprite, rectangle, 15.0f);
        }

        public override void Update(GameTime gameTime, Vector2 drawPos)
        {
            lastPos = new Vector2(X, Y);
            
            if (!Hit)
            {
                doMovement();
            }
            else
            {
                gotHit(gameTime);
            }
            sprite.Position = drawPos;
            sprite.Update(gameTime);
        }

        public void gotHit(GameTime gameTime)
        {
            if (hitGameTime == -1)
            {
                playerInfo.Health -= 25;
                if (playerInfo.Health <= 0)
                {
                    rectangle = initRectangle;
                    playerInfo.Health = playerInfo.InitHealth;
                }
                hitGameTime = gameTime.ElapsedGameTime.Milliseconds;
            }
            if (blinked <= blinkingTime)
            {
                blinked += gameTime.ElapsedGameTime.Milliseconds;
            }
            else
            {
                blinked = 0;
                hitGameTime = -1;
                hit = false;
            }
        }

        public void doMovement()
        {
            if (onBottom)
            {
                amIJumping = false;
            }

            curKeyState = Keyboard.GetState();
            if (curKeyState.IsKeyDown(Keys.Left))
            {
                X -= Speed;
            }
            if (curKeyState.IsKeyDown(Keys.Right))
            {
                X += Speed;
            }
            if (curKeyState.IsKeyDown(Keys.Space) && !amIJumping && onBottom)
            {
                amIJumping = true;
            }
            if (amIJumping)
            {
                Y -= (int)jumping;
                jumping -= 0.5f;
            }
            else
            {
                jumping = jumpForce;
            }
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            int timesToBlink = 3;
            if (hit)
            {
                if (blinked % (blinkingTime / timesToBlink) > blinkingTime / (timesToBlink * 2))
                {
                    sprite.Draw(spriteBatch);
                }
            }
            else
            {
                sprite.Draw(spriteBatch);
            }
            playerInfo.Draw(spriteBatch);
        }
    }
}
